package com.pet.system.controller;

import com.pet.common.constant.ResponseCode;
import com.pet.common.model.PageModel;
import com.pet.common.model.ResponseResult;
import com.pet.system.domain.entity.SysApp;
import com.pet.system.domain.vo.SysAppVO;
import com.pet.system.service.ISysAppService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 应用控制器
 *
 * @author Centaurea
 */
@RestController
@RequestMapping("app")
@AllArgsConstructor
public class AppController {
  private final ISysAppService appService;

  @PostMapping
  public ResponseResult add(SysApp entity) {
    entity.setId(null);
    boolean flag = appService.save(entity);
    return flag ? ResponseResult.success(ResponseCode.WEB_200) : ResponseResult.error();
  }

  @PutMapping
  public ResponseResult update(SysApp entity) {
    boolean flag = appService.update(entity);
    return flag ? ResponseResult.success(ResponseCode.WEB_200) : ResponseResult.error();
  }

  @DeleteMapping("/{ids}")
  public ResponseResult delete(@PathVariable("ids") String ids) {
    if (StringUtils.isEmpty(ids)) {
      return ResponseResult.error("参数不合法");
    }
    List<Integer> args = new ArrayList<>();
    Arrays.stream(ids.split(",")).forEach(id -> args.add(Integer.valueOf(id)));
    boolean flag = appService.delete(args);
    return flag ? ResponseResult.success(ResponseCode.WEB_200) : ResponseResult.error();
  }

  @GetMapping
  public ResponseResult page(PageModel<SysApp> page, SysAppVO params) {
    if (page == null) {
      page = new PageModel<>();
    }
    PageModel<SysApp> query = appService.query(page, params);
    return ResponseResult.success(ResponseCode.WEB_200, query);
  }
}
